{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 60,
   "id": "35fa89f1-afd2-4e57-b97b-35e07d3842c8",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "stop\n"
     ]
    }
   ],
   "source": [
    "import socket\n",
    "import sys\n",
    "import os\n",
    "import time\n",
    "from robodyno import Robot\n",
    "from robodyno.nodes import StepperBoardFactory\n",
    "robot = Robot()\n",
    "device_id = 0x22\n",
    "stepper_board = robot.add_device('stepper', StepperBoardFactory(), device_id, reduction = 10)\n",
    "stepper_board.enable()\n",
    "\n",
    "track_sock = './track.sock'\n",
    "\n",
    "if os.path.exists(track_sock):\n",
    "    os.unlink(track_sock)\n",
    "    \n",
    "sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)\n",
    "sock.bind(track_sock)\n",
    "sock.listen(1)\n",
    "conn, clientAddr = sock.accept()\n",
    "\n",
    "time.sleep(1)\n",
    "stepper_board.set_vel(-1.5)\n",
    "\n",
    "try:\n",
    "    while True:\n",
    "        data = conn.recv(10)\n",
    "        print(data.decode())\n",
    "        if data.decode() == 'stop':\n",
    "            stepper_board.set_vel(0)\n",
    "            break\n",
    "except Exception as e:\n",
    "    print(e)\n",
    "    pass\n",
    "finally:\n",
    "    conn.close()\n",
    "    sock.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "64eeccc6-8536-400c-9fff-9b38c10cc473",
   "metadata": {},
   "outputs": [],
   "source": [
    "stepper_board.set_vel(0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0adbcdba-7062-4c02-9c03-d5ba638db864",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
